package com.meihua.baoleyi.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.meihua.baoleyi.R;
import com.meihua.baoleyi.base.BaseActivity;
import com.meihua.baoleyi.common.APIServer;
import com.meihua.baoleyi.common.MyApp;
import com.meihua.baoleyi.common.PrefsHelper;
import com.meihua.baoleyi.model.LoginInfo;
import com.meihua.baoleyi.net.NetHelper;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.TreeMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 *  用户名界面
 */
public class ChangeUNameActivity extends BaseActivity {

    private static final String ACTION_NAME = "PayDialog";
    @Bind(R.id.text_title)
    TextView textTitle;
    @Bind(R.id.et_name)
    EditText etName;
    @Bind(R.id.tv_submit)
    TextView tvSubmit;
    private String name;

    /**
     * 创建跳转Intent
     * @param name 用户名
     * @return
     */
    public static Intent createIntent(Context mContext, String name) {
        Intent intent = new Intent(mContext, ChangeUNameActivity.class);
        intent.putExtra("name", name);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_uname);
        ButterKnife.bind(this);
        //设置已有用户名
        name = getIntent().getStringExtra("name");
        etName.setText(name);
        textTitle.setText("用户名");
    }

    /**
     * 点击事件
     * @param view
     */
    @OnClick({R.id.text_back, R.id.tv_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.text_back://返回
                finish();
                break;
            case R.id.tv_submit://提交
                if (TextUtils.isEmpty(etName.getText().toString().trim())) {
                    Toast.makeText(this, "请输入姓名", Toast.LENGTH_SHORT).show();
                    return;
                }
                chageName();//修改用户名
                break;
        }
    }

    /**
     * 修改用户名
     */
    private void chageName() {
        TreeMap<String, String> params = new TreeMap();
        params.put("uname", etName.getText().toString().trim());//用户名
        params.put("user_id", MyApp.getInstance().getUser_id());//用户id
        NetHelper.post(APIServer.CHANGENAME, params, true, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                JSONObject obj = null;
                try {
                    obj = new JSONObject(s);
                    if (obj.getString("code").equals("200")) {
                        //上一个界面更新
                        Intent intent = new Intent();
                        intent.putExtra("name", etName.getText().toString().trim());
                        setResult(RESULT_OK, intent);
                        //我的界面用户名更新
                        Intent mIntent = new Intent(ACTION_NAME);
                        sendBroadcast(mIntent);
                        //MyAPP保存的信息更新
                        MyApp.getInstance().setUname(etName.getText().toString().trim());
                        //SP保存的用户信息更新
                        LoginInfo loginInfo =  NetHelper.getBeanForJson(LoginInfo.class, PrefsHelper.getLoginInfo());
                        loginInfo.setUname(etName.getText().toString().trim());
                        PrefsHelper.setLoginInfo(loginInfo);
                        finish();
                    }
                    Toast.makeText(ChangeUNameActivity.this, obj.getString("msg"), Toast.LENGTH_SHORT).show();
                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(ChangeUNameActivity.this, "服务器异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

}
